//
//  MCViewController.m
//  MCNetworking
//
//  Created by 辛金巳火戊己土 on 08/17/2020.
//  Copyright (c) 2020 辛金巳火戊己土. All rights reserved.
//

#import "MCViewController.h"
#import <MCNetworking/MCNetworking.h>
#import "MCCommonRequest.h"

@interface MCViewController ()

@end

@implementation MCViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	
    self.view.backgroundColor = UIColor.whiteColor;
    
    [[MCNetworking sharedInstance] setTimeoutInterval:10];
    [[MCNetworkMonitoring sharedInstance] startMonitoring];

    [[MCNetworking sharedInstance] setBaseURLString:@"http://192.168.2.191:8881/"];

    // NSString *userToken = [CSManager sharedInstance].userToken;
    NSString *userToken = @"userToken";
    [[MCNetworking sharedInstance] setHeaderValue:userToken forKey:@"userToken"];

    // NSString *uuid = [CSManager sharedInstance].uuid;
    NSString *uuid = @"uuid";
    [[MCNetworking sharedInstance] setHeaderValue:uuid forKey:@"uuid"];

    /**
     服务器返回JSON字典需要解析的key 当 [MCResponseTool analyzeDataWith... modelClass:nil ...] modelClass 为 nil 时 则不会进行解析
     */
    [[MCNetworking sharedInstance] setResponseObjectCodeKey:@"code" msgKey:@"message" dataKey:@"data"];
    
    /**
     需要面向用户的结果码 MCAnalyzedResult *result
        if ([result isExposedToUser]) { // (20001, 29999)
            [result show];
        }
     */
    [[MCNetworking sharedInstance] setMCResultCodeDigitalRange:MCResultCodeDigitalRangeMake(20001, 29999)];

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
//    [MCTip showLoading:@"正在获取userToken..."];
    [self requestUserToken];
}

- (void)requestUserToken {
    NSString *uuid = @"669cbb9ccfea0562f349ee6f8b2e75d0";
    [MCCommonRequest getUserTokenWithUUID:uuid resultBlock:^(MCAnalyzedResult *result, id object) {
        if (result.resultCode == MC_RESULT_SUCCESS) {
            NSString *userToken = object;
            // save token
            NSLog(@"userToken获取完毕 = %@", userToken);
            // header 中拼接 参数
            [[MCNetworking sharedInstance] setHeaderValue:userToken forKey:@"userToken"];
            
        }else {
            NSLog(@"userToken获取失败: %@ ",result.resultDesc);
            if ([result isExposedToUser]) {
                [result show];
            }
        }
    }];
}


@end
